// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using System.Linq;
using NUnit.Framework;
using ValidationAspects;
using ValidationAspects.Exceptions;
using ValidationAspects.Factories;

namespace TestValidationAspects
{
	[TestFixture]
	public class TestMatchesRegexAttribute
	{
		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void Test_constructor_throws_for_null_regex()
		{
			new MatchesRegexAttribute(null);
		}

		[Test]
		[ExpectedException(typeof(ArgumentNullException))]
		public void Test_constructor_throws_for_empty_regex()
		{
			new MatchesRegexAttribute(string.Empty);
		}

		[Test]
		public void Test_get_Factory()
		{
			MatchesRegex factory = new MatchesRegexAttribute("pattern") {Name = "my pattern"}.Factory as MatchesRegex;
			Assert.IsNotNull(factory);
			Assert.AreEqual("pattern", factory.Regex);
			Assert.AreEqual("my pattern", factory.Name);
		}

		[Test]
		public void Test_usage()
		{
			Dummy dummy = new Dummy {MatchesRegex = "a"};

			ValidationResult result = dummy.Validate();
			Assert.IsFalse(result.IsValid);
			Assert.AreSame(typeof(ValidateMatchesRegexException), ((ValidationContextException)result.ValidationException).Flatten().First().GetType());
		}

		#region Mocks
		public class Dummy
		{
			[MatchesRegex("@[0-9]{1,10}", Name = "Customer Id")]
			public string MatchesRegex { get; set; }
		}
		#endregion
	}
}